package com.ymm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ymm.domain.entity.Menu;
import com.ymm.domain.entity.RoleMenu;
import com.ymm.mapper.RoleMenuMapper;
import com.ymm.service.RoleMenuService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色和菜单关联表(RoleMenu)表服务实现类
 *
 * @author 椰咩咩咩
 * @since 2023-04-03 15:11:45
 */
@Service("roleMenuService")
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Override
    public List<Long> getMidByRoles(List<Long> roles) {
        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(RoleMenu::getRoleId, roles);
        List<RoleMenu> roleMenus = list(wrapper);
        List<Long> mIds = roleMenus.stream().map(roleMenu -> roleMenu.getMenuId()).collect(Collectors.toList());
        return mIds;
    }

    @Override
    public List<Long> getRoleHasMenu(Long roleID) {
        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleMenu::getRoleId, roleID);
        List<RoleMenu> roleMenus = list(wrapper);
        List<Long> mIds = roleMenus.stream().map(roleMenu -> roleMenu.getMenuId()).collect(Collectors.toList());
        return mIds;
    }

    @Override
    public void delMenuByRole(RoleMenu roleMenu) {
        LambdaUpdateWrapper<RoleMenu> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RoleMenu::getRoleId, roleMenu.getRoleId());
        wrapper.eq(RoleMenu::getMenuId, roleMenu.getMenuId());
        remove(wrapper);
    }
}

